<?php

namespace App\Http\Controllers\Activity;

use App\Http\Controllers\Controller;
use App\Models\Activity;
use App\Models\Tools\Data;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

class ActivityStoreController extends Controller
{
    /**
     * 查询活动列表
     */

    public function index()
    {

        $all = $this->all;

        $pageSize = $all['pageSize'] ?? 20;
        $pageNo = $all['pageNo'] ?? 1;

        $where = function ($query) use ($all) {
            //状态，默认全部
            $status = $all['status'] ?? [];
            if(!count($status)){
                $status = array_keys(Data::$lotteryActivityStatus);
            }
            $query->whereIn('la.status', $status);

            // 店铺名筛选
            if (isset($all['shop_name'])) {
                $query->where('shop.shop_name', '=', $all['shop_name']);
            }

            // 核销超期
            if(isset($all['consume']) && $all['consume']){
                $query->where('la.draw_time','<',date('Y-m-d H:i:s',Activity::overTime()))
                    ->where('la.status','=',Data::DATA_DB_LOTTERY_ACTIVITY_STATUS_AFTER);
            }

//            if (isset($all['openid'])) {
//                $query->where('es.openid', '=', $all['openid']);
//            }

//            if (isset($all['date']) && $all['date'] !== []) {
//                $date = [$all['date'][0] . ' 00:00:00', $all['date'][1] . ' 23:59:59'];
//                $query->whereBetween('es.created_at', $date);
//            }
        };

        $total = DB::table('lottery_activity as la')
            ->leftJoin('shop', 'la.shop_id', '=', 'shop.shop_id')
            ->where($where)
            ->count();

        $result = DB::table('lottery_activity as la')
            ->leftJoin('lottery_goods as lg', 'la.goods_id', '=', 'lg.goods_id')
            ->leftJoin('lottery_users as lu', 'la.creator', '=', 'lu.we_id')
            ->leftJoin('shop', 'la.shop_id', '=', 'shop.shop_id')
            ->leftJoin('dict_area as da', 'shop.district', '=', 'da.id')
            ->leftJoin('lottery_draw_detail as ldd','la.act_id','=','ldd.act_id')
            ->where($where)
            ->orderByDesc('la.act_id')
            ->skip(($pageNo - 1) * $pageSize)
            ->take($pageSize)
            ->select('la.act_id', 'la.act_no', 'la.goods_id', 'la.start_time', 'la.end_time', 'la.draw_time', 'la.status', 'la.is_stop',
                'shop.shop_id', 'shop_name', 'shop_type', 'da.path', 'da.short_name',
                'lg.goods_name', 'lu.nickname as creator_name')
            ->get()
            ->map(function($item){
                $item->shop_type_name = Data::$laifeiShopTypes[$item->shop_type];
                return $item;
            })
            ->toArray();
        $result = json_decode(json_encode($result), true);


        $act_ids = Arr::pluck($result, 'act_id');
        $count = DB::table('lottery_users_draw')
            ->whereIn('act_id', $act_ids)
            ->groupBy('act_id')
            ->selectRaw('act_id,COUNT(*) as count')
            ->get()
            ->mapWithKeys(function ($item) {
                return [$item->act_id => $item->count];
            })
            ->toArray();
        $count = json_decode(json_encode($count), true);

        foreach ($result as &$value) {
            $value['status_name'] = Data::$lotteryActivityStatus[$value['status']];
            $value['count'] = $count[$value['act_id']] ?? 0;
        }

        return $this->setReturnJson(0, 'success', ['total' => $total, 'info' => $result]);

    }

}
